//
//  CommendViewController.m
//  govern
//
//  Created by LiangLigeng on 14-8-18.
//  Copyright (c) 2014年 LiangLigeng. All rights reserved.
//

#import "CommendViewController.h"
//#import "HTMLParser.h"
//#import "FDLabelView.h"
//#import <MediaPlayer/MediaPlayer.h>
#import "UIImageView+WebCache.h"
#import "AFHTTPRequestOperationManager.h"
#import "DTCoreText.h"

@interface CommendViewController ()

@end

@implementation CommendViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"推荐";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //导航栏背景
    //UIImage *navBack=[UIImage imageNamed:@"p2_top_bar.png"];
    //[self.navigationController.navigationBar setBackgroundImage:navBack forBarMetrics:UIBarMetricsDefault];
    
    mScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:mScrollView];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://222.85.136.55:8033/api/getnewscontent?contentid=11bfca80-b51a-4579-8322-d7e5f32e77cd" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *contents = (NSDictionary *)responseObject;
        NSString *content=[contents objectForKey:@"Content"];
        
        
        NSString *html=[content stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [self loadHtmlView:html];
        //html = [html stringByReplacingOccurrencesOfString:@"<br/>" withString:@""];
        //html = [html stringByReplacingOccurrencesOfString:@"+" withString:@" "];
        //NSLog(@"JSON: %@", html);
        //NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_2312_80);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    
    CGRect frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height);
    
    // Create text view
	_textView = [[DTAttributedTextView alloc] initWithFrame:frame];
	
	// we draw images and links via subviews provided by delegate methods
	_textView.shouldDrawImages = NO;
	_textView.shouldDrawLinks = NO;
	_textView.textDelegate = self; // delegate for custom sub views
	
	// gesture for testing cursor positions
	UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
	[_textView addGestureRecognizer:tap];
	
	// set an inset. Since the bottom is below a toolbar inset by 44px
	[_textView setScrollIndicatorInsets:UIEdgeInsetsMake(0, 0, 44, 0)];
	_textView.contentInset = UIEdgeInsetsMake(10, 10, 54, 10);
    
	_textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	[self.view addSubview:_textView];
    

    
    

}

-(void)loadHtmlView:(NSString *)html
{
    //NSString *html = @"<p>Some Text</p>";
    NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
    
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTMLData:data documentAttributes:NULL];
    NSLog(@"%@", attrString);
    _textView.shouldDrawLinks = NO; // we draw them in DTLinkButton
	_textView.attributedString = attrString;
}


#pragma mark - DTLazyImageViewDelegate

- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size {
	NSURL *url = lazyImageView.url;
	CGSize imageSize = size;
	
	NSPredicate *pred = [NSPredicate predicateWithFormat:@"contentURL == %@", url];
	
	BOOL didUpdate = NO;
	
	// update all attachments that matchin this URL (possibly multiple images with same size)
	for (DTTextAttachment *oneAttachment in [_textView.attributedTextContentView.layoutFrame textAttachmentsWithPredicate:pred])
	{
		// update attachments that have no original size, that also sets the display size
		if (CGSizeEqualToSize(oneAttachment.originalSize, CGSizeZero))
		{
			oneAttachment.originalSize = imageSize;
			
			didUpdate = YES;
		}
	}
	
	if (didUpdate)
	{
		// layout might have changed due to image sizes
		[_textView relayoutText];
	}
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
